package com.fypg.module.medical.controller.admin.registerorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.CommonResult;
import com.fypg.framework.common.util.object.BeanUtils;
import static com.fypg.framework.common.pojo.CommonResult.success;

import com.fypg.framework.excel.core.util.ExcelUtils;

import com.fypg.framework.apilog.core.annotation.ApiAccessLog;
import static com.fypg.framework.apilog.core.enums.OperateTypeEnum.*;

import com.fypg.module.medical.controller.admin.registerorder.vo.*;
import com.fypg.module.medical.dal.dataobject.registerorder.RegisterOrderDO;
import com.fypg.module.medical.service.registerorder.RegisterOrderService;

@Tag(name = "管理后台 - 挂号订单信息")
@RestController
@RequestMapping("/medical/register-order")
@Validated
public class RegisterOrderController {

    @Resource
    private RegisterOrderService registerOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建挂号订单信息")
    @PreAuthorize("@ss.hasPermission('medical:register-order:create')")
    public CommonResult<String> createRegisterOrder(@Valid @RequestBody RegisterOrderSaveReqVO createReqVO) {
        return success(registerOrderService.createRegisterOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新挂号订单信息")
    @PreAuthorize("@ss.hasPermission('medical:register-order:update')")
    public CommonResult<Boolean> updateRegisterOrder(@Valid @RequestBody RegisterOrderSaveReqVO updateReqVO) {
        registerOrderService.updateRegisterOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除挂号订单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('medical:register-order:delete')")
    public CommonResult<Boolean> deleteRegisterOrder(@RequestParam("id") String id) {
        registerOrderService.deleteRegisterOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除挂号订单信息")
                @PreAuthorize("@ss.hasPermission('medical:register-order:delete')")
    public CommonResult<Boolean> deleteRegisterOrderList(@RequestParam("ids") List<String> ids) {
        registerOrderService.deleteRegisterOrderListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得挂号订单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medical:register-order:query')")
    public CommonResult<RegisterOrderRespVO> getRegisterOrder(@RequestParam("id") String id) {
        RegisterOrderDO registerOrder = registerOrderService.getRegisterOrder(id);
        return success(BeanUtils.toBean(registerOrder, RegisterOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得挂号订单信息分页")
    @PreAuthorize("@ss.hasPermission('medical:register-order:query')")
    public CommonResult<PageResult<RegisterOrderRespVO>> getRegisterOrderPage(@Valid RegisterOrderPageReqVO pageReqVO) {
        PageResult<RegisterOrderDO> pageResult = registerOrderService.getRegisterOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RegisterOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出挂号订单信息 Excel")
    @PreAuthorize("@ss.hasPermission('medical:register-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRegisterOrderExcel(@Valid RegisterOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RegisterOrderDO> list = registerOrderService.getRegisterOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "挂号订单信息.xls", "数据", RegisterOrderRespVO.class,
                        BeanUtils.toBean(list, RegisterOrderRespVO.class));
    }

}